package com.yunkaweilai.android.activity.recharge;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.fragment.recharge.MessageRechargeFragment;
import com.yunkaweilai.android.fragment.recharge.ShopRechargeFragment;
import com.yunkaweilai.android.utils.TitleBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/2/19.
 */

public class RenewalRechargeActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_rbtn_1)
    RadioButton idRbtn1;
    @BindView(R.id.id_rbtn_2)
    RadioButton idRbtn2;
    @BindView(R.id.id_group)
    RadioGroup idGroup;
    @BindView(R.id.id_view1)
    View idView1;
    @BindView(R.id.id_view2)
    View idView2;
    @BindView(R.id.id_shop_container)
    FrameLayout idShopContainer;
    private ShopRechargeFragment shopRechargeFragment;
    private MessageRechargeFragment messageRechargeFragment;
    public static final String TYPE_NAME = "TYPE_NAME";
    public static final String IS_TOKEN = "IS_TOKEN";
    private String typeName;
    private boolean istoken;

    public static void actionStart(Context context, String typeName, boolean istoken){
        Intent intent = new Intent(context,RenewalRechargeActivity.class);
        intent.putExtra(TYPE_NAME,typeName);
        intent.putExtra(IS_TOKEN,istoken);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.renewal_recharge_layout);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("充值续费")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        typeName = getIntent().getStringExtra(TYPE_NAME);
        istoken = getIntent().getBooleanExtra(IS_TOKEN,true);
        initClick();
//        idGroup.setVisibility(View.GONE);//todo:k1 华为上架更改
    }

    private void initClick() {
        idGroup.setOnCheckedChangeListener(this);
        idRbtn1.setChecked(true);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (checkedId) {
            case R.id.id_rbtn_1:
                if (shopRechargeFragment == null) {
                    shopRechargeFragment = ShopRechargeFragment.newInstence(typeName,istoken);
                    transaction.add(R.id.id_shop_container, shopRechargeFragment);
                } else {
                    transaction.show(shopRechargeFragment);
                }
                idView1.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                idView2.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                break;
            case R.id.id_rbtn_2:
                if (messageRechargeFragment == null) {
                    messageRechargeFragment = MessageRechargeFragment.newInstence(typeName,istoken);
                    transaction.add(R.id.id_shop_container, messageRechargeFragment);
                } else {
                    transaction.show(messageRechargeFragment);
                }
                idView2.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                idView1.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                break;
        }
        transaction.commit();
    }
    public void hideAllFragment(FragmentTransaction transaction) {
        if (shopRechargeFragment != null) {
            transaction.hide(shopRechargeFragment);
        }
        if (messageRechargeFragment != null) {
            transaction.hide(messageRechargeFragment);
        }
    }
}
